package com.huidz.calendar.activityadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huidz.calendar.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HotJieRiAdapter extends BaseAdapter {
    ArrayList<String> list;
    ArrayList<String> lists;
    Context context;

    public HotJieRiAdapter(ArrayList<String> list, ArrayList<String> lists, Context context) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.lists = lists;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.jieri_list, null);
        init i = new init();
        i.all = convertView.findViewById(R.id.all);
        i.week = convertView.findViewById(R.id.week);
        i.bwl_list_msg = convertView.findViewById(R.id.bwl_list_msg);
        i.bwl_list_msg_time = convertView.findViewById(R.id.bwl_list_msg_time);
        i.bwl_list_time = convertView.findViewById(R.id.bwl_list_time);
        i.bwl_list_msg.setText(lists.get(position));
        i.bwl_list_msg_time.setText(list.get(position));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //创建对象制定日期格式
        String dates = format.format(new Date());
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy"); //创建对象制定日期格式
        String date1s = format1.format(new Date());
        if (dateDiff(dates, date1s + list.get(position)) > 0)
            i.week.setText(dateDiff(dates, date1s + list.get(position)) + "天");
        else
            i.week.setText("已过期");
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView bwl_list_msg, bwl_list_msg_time, bwl_list_time, week;
        RelativeLayout all;
    }

    public long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            if (day >= 1) {
                return day;
            } else {
                if (day == 0) {
                    return 1;
                } else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }


}

